﻿using Esponce.QRCode;
using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using Windows.Media.Capture;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace Esponce.Models
{
	public class MainViewModel : INotifyPropertyChanged
	{
		public string Content
		{
			get
			{
				return this.content;
			}
			set
			{
				if (this.content != value)
				{
					this.content = value;
					OnPropertyChanged("Content");
				}
			}
		}
		private string content;

		public ImageSource Image
		{
			get
			{
				return this.image;
			}
			set
			{
				if (this.image != value)
				{
					this.image = value;
					OnPropertyChanged("Image");
				}
			}
		}
		private ImageSource image;

		public bool IsLoading
		{
			get
			{
				return this.isLoading;
			}
			set
			{
				if (this.isLoading != value)
				{
					this.isLoading = value;
					OnPropertyChanged("IsLoading");
				}
			}
		}
		private bool isLoading;

		public async Task GenerateAsync()
		{
			var client = new QRCodeClient();

			//Generate a QR Code PNG image throught Esponce web service
			using (var stream = await client.GenerateAsync(this.Content))
			{
				using (var ms = new InMemoryRandomAccessStream())
				{
					int size = 0;
					var buffer = new byte[1024];

					var s = ms.AsStreamForWrite();
					while ((size = stream.Read(buffer, 0, 1024)) != 0)
					{
						s.Write(buffer, 0, size);
					}

					s.Position = 0;

					//Create BitmapImage with QR Code
					var image = new BitmapImage();
					image.SetSource(ms);

					//Attach to the UI
					this.Image = image;
				}
			}
		}

		public async Task<string> CapturePictureAsync()
		{
			var dialog = new CameraCaptureUI();
			dialog.PhotoSettings.AllowCropping = true;
			dialog.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.MediumXga;

			var file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
			if (file != null)
			{
				var stream = await file.OpenAsync(FileAccessMode.Read);
				string content = await DecodeStreamAsync(stream);
				return content;
			}

			return null;
		}

		public async Task<string> BrowserFileAsync()
		{
			var picker = new FileOpenPicker();
			picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
			picker.ViewMode = PickerViewMode.List;
			picker.FileTypeFilter.Add(".png"); //QR Code

			var file = await picker.PickSingleFileAsync();
			if (file != null)
			{
				var stream = await file.OpenAsync(FileAccessMode.Read);
				string content = await DecodeStreamAsync(stream);
				return content;
			}

			return null;
		}

		protected async Task<string> DecodeStreamAsync(IRandomAccessStream stream)
		{
			var rs = stream.AsStreamForRead();

			var client = new QRCodeClient();
			var content = await client.DecodeAsync(rs);

			return content;
		}

		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
